
package com.ttc.mBackup.BackupAgent;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;

import com.dropbox.client.DropboxAPI;
import com.dropbox.client.DropboxClient;


public class LoginAsyncTask extends AsyncTask<Void, Void, Integer> {
    private static final String TAG = "LoginAsyncTask";

    String mUser;
    String mPassword;
    String mErrorMessage="";
    LoginDropbox mDropboxSample;
    LoginAddTask mDropbox;
    DropboxAPI.Config mConfig;
    DropboxAPI.Account mAccount;
    boolean check=false;
    /*Value of name and key SharedPreference*/
	
    // Will just log in
    public LoginAsyncTask(LoginDropbox act, String user, String password, DropboxAPI.Config config) {
        super();
      
        
        
        mDropboxSample = act;
        mUser = user;
        mPassword = password;
        mConfig = config;
    }
    // Will just log in
    public LoginAsyncTask(LoginAddTask act, String user, String password, DropboxAPI.Config config,boolean check) {
        super();
        mDropbox = act;
        mUser = user;
        mPassword = password;
        mConfig = config;
        this.check=check;
    }

    @Override
    protected Integer doInBackground(Void... params) {
    	
        try {
        	DropboxAPI api = check?mDropbox.getAPI():mDropboxSample.getAPI();
        	
        	int success = DropboxAPI.STATUS_NONE;
        	if (!api.isAuthenticated()) {
        		
	            mConfig = api.authenticate(mConfig, mUser, mPassword);
	            if(check)
	            {  mDropbox.setConfig(mConfig);}
	            else
	            {  mDropboxSample.setConfig(mConfig);}
            
	          
            
	            success = mConfig.authStatus;

	            if (success != DropboxAPI.STATUS_SUCCESS) {
	            	return success;
	            }
        	}
        	mAccount = api.accountInfo();

        	if (!mAccount.isError()) {
        		return DropboxAPI.STATUS_SUCCESS;
        	} else {
        		Log.e(TAG, "Account info error: " + mAccount.httpCode + " " + mAccount.httpReason);
        		return DropboxAPI.STATUS_FAILURE;
        	}
        } catch (Exception e) {
            Log.e(TAG, "Error in logging in.", e);
            return DropboxAPI.STATUS_NETWORK_ERROR;
        }
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (result == DropboxAPI.STATUS_SUCCESS) {
        	if (mConfig != null && mConfig.authStatus == DropboxAPI.STATUS_SUCCESS) {
            	
        		if(check)
	            { mDropbox.storeKeys(mConfig.accessTokenKey, mConfig.accessTokenSecret);
            	mDropbox.setLoggedIn(true);
            	mDropbox.showToast("Logged into Dropbox");}
	            else
	            { mDropboxSample.storeKeys(mConfig.accessTokenKey, mConfig.accessTokenSecret);
            	mDropboxSample.setLoggedIn(true);
            	
            	mDropboxSample.showToast("Logged into Dropbox");
            	}
        		
            	
            }
        	
        } else {
        	if (result == DropboxAPI.STATUS_NETWORK_ERROR) {
        		if(check)
	            { mDropbox.showToast("Network error: " + mConfig.authDetail);
	            mDropbox.setLoggedIn(false);
            	}
	            else
	            { 
	            	mDropboxSample.showToast("Network error: " + mConfig.authDetail);
	            	mDropboxSample.setLoggedIn(false);
	            }
        	} else {
        		if(check)
	            { mDropbox.showToast("Unsuccessful login.");
	            	mDropbox.setLoggedIn(false);}
	            else
	            { 
	            	mDropboxSample.showToast("Unsuccessful login.");
	            	mDropboxSample.setLoggedIn(false);
	            }
        	}
        }
    }

}
